 var app = new Vue({
   el: '#app',
   mixins: [
     tablePaginationMixin()
   ],
   data: function () {
     // 搜索条件
     return {
       search: {
         searchText: '',
         view: 1
       },
       oldSearch: {
         searchText: '',
         view: ''
       },
       deptList: [],
       materialTypeList: [],
       workShopList: [],
       addWorkList: [],
       processList: [],
       personList: [], //人员原始记录
       adminProcessList: [], //导入工序数据
       adminProcessIdList: [], //导入工序id
       updateProcessDetail: {
         id: '',
         sort: 0,
         productionLine: 0,
         workshopName: 700,
         processName: '',
         status: '',
         chargeUnit: '',
         timeUnit: '',
         unitPrice: 0.0,
         processCode: '',
         creater: '',
         creationTime: '',
         lastModifier: '',
         lastModifyTime: '',
         workingProcedureDuration: 0,
         auditor: '',
         auditTime: '',
         orderNumber: 0,
         processingObject: '',
         isNeedReviewed: 1,
         reviewerList: [],
         processingObjects: [],
         pageWorkShopName: '',
         pageDeptName: '',
         reviewerUserId: [],
         handoverTime: 0,
         qcReviewerIds: '',
         reportMode: '',
         reportModeRecord: ''
       },
       addProcessDetail: {
         sort: 0,
         productionLine: 0,
         workshopName: 0,
         processName: '',
         status: '使用',
         chargeUnit: '付',
         timeUnit: '付',
         unitPrice: 0.0,
         processCode: '',
         workingProcedureDuration: 0,
         processingObject: '',
         isNeedReviewed: 1,
         reviewerList: [],
         processingObjects: [],
         reviewerUserId: [],
         handoverTime: 0,
         qcReviewerIds: '',
         reportModeRecord: 1
       },
       materialData: [], //修改时穿梭框物料数据
       personData: [], //用户穿梭框数据
       loading: false,
       updateDialog: false,
       updateSetUserDialog: false,
       addDialog: false,
       addMaterialDialog: false,
       addSetUserDialog: false,
       importDialog: false,
       moment: moment,
       selectProcess: null,
       openSetting: false,
       reportModeList: i18n.getDropDown('workReport.reportMode', [1, 2])
     };
   },
   created: function () {
     this.loading = true;
     var self = this;
     Promise.all([
       this.setProcessList(true),
       this.getWorkShopAndDeptList(),
       this.getMaterialTypeList(),
       this.getPersonList(),
       this.getAdminProcess()
     ]).then(function () {
       self.loading = false;
     }).catch(function () {
       self.loading = false;
     });

   },
   methods: {
     filterMethod: function (query, item) {
       return item.label.indexOf(query) > -1;
     },
     addSetUserChange: function () {
       var self = this;
       var useIdList = self.addProcessDetail.reviewerUserId;
       var array = _.filter(self.personList, function (per) {
         return useIdList.indexOf(per.id) != -1;
       });
       self.addProcessDetail.reviewerList = _.map(array, function (o) {
         return {
           approverId: o.id,
           approverName: o.personName
         };
       });
       self.addProcessDetail.qcReviewerIds = '';
     },
     updateSetUserChange: function () {
       var self = this;
       var useIdList = self.updateProcessDetail.reviewerUserId;
       var array = _.filter(self.personList, function (per) {
         return useIdList.indexOf(per.id) != -1;
       });
       self.updateProcessDetail.reviewerList = _.map(array, function (o) {
         return {
           approverId: o.id,
           approverName: o.personName
         };
       });
     },
     getAdminProcess: function () {
       var self = this;
       return ajaxRequest("basicData/getAdminProcess", {
         'searchText': ''
       }).then(function (res) {
         var array = [];
         for (var i = 0; i < res.length; i++) {
           var one = {};
           one.key = res[i].id;
           one.label = res[i].productionLine + ' ' + res[i].workshopName + ' ' + res[i].processName;
           array.push(one);
         }
         self.adminProcessList = array;
         return array;
       });
     },
     //获取公司所有人员
     getPersonList: function () {
       var self = this;
       return ajaxRequest("basicData/getPersonName", {
         'view': '1'
       }).then(function (res) {
         self.personList = res;
         var array = [];
         for (var i = 0; i < res.length; i++) {
           var one = {};
           one.key = res[i].id;
           one.label = res[i].personName;
           array.push(one);
         }
         self.personData = array;
         return array;
       });
     },
     //获取所有物料类型
     getMaterialTypeList: function () {
       var self = this;
       return ajaxRequest("basicData/getRowMaterialType", {
         'view': '1'
       }).then(function (res) {
         res.push("款");
         self.materialTypeList = res;
         var array = [];
         for (var i = 0; i < res.length; i++) {
           var one = {};
           one.key = res[i];
           one.label = res[i];
           array.push(one);
         }
         self.materialData = array;
         return array;
       });
     },
     //获取车间和部门
     getWorkShopAndDeptList: function () {
       var self = this;
       return ajaxRequest("basicData/getWorkshopName", {
         'view': '1'
       }).then(function (res) {
         self.deptList = _.uniqBy(res, "deptId");
         self.workShopList = _.groupBy(res, "deptId");
         return res;
       });
     },
     //获取所有工序
     setProcessList: function (isSearch) {
       var self = this;
       var search = this.search;
       if (isSearch) {
         this.oldSearch = _.cloneDeep(search);
         this.pagination.currentPage = 1;
       }
       search = _.cloneDeep(this.oldSearch);
       search.start = (this.pagination.currentPage - 1) * this.pagination.pageSize;
       search.length = this.pagination.pageSize;
       return ajaxRequest("basicData/queryProcess", search, false).then(function (res) {
         if (!isSearch) {
           self.search = _.cloneDeep(self.oldSearch);
         }

         var processList = res.returnData;
         self.processList = processList;
         self.pagination.totalCount = res.iTotalRecords;
         return processList;
       });
     },
     resetProcessList: function (isSearch) {
       this.loading = true;
       var self = this;
       this.setProcessList(isSearch).then(function () {
         self.loading = false;
       }).catch(function () {
         self.loading = false;
       });
     },
     //点击分页
     pageChange: function () {
       this.resetProcessList(false);
     },
     //获取工序详情
     getProcessDetail: function (row) {
       var id = row.id;
       var self = this;
       ajaxRequest("basicData/getProcessDetail", {
         'id': id
       }).then(function (res) {
         if (res.length <= 0) {
           self.updateProcessDetail = {};
           return;
         }
         res = res[0];
         var pageDeptName = '';
         var pageWorkShopName = "";
         var dept = _.filter(self.deptList, {
           'deptId': res.productionLine + ''
         });
         if (dept.length > 0) {
           pageDeptName = dept[0].productionLine;
         }
         var workList = self.workShopList[res.productionLine];
         workList = _.filter(workList, {
           'id': res.workshopName + ''
         });
         if (workList.length > 0) {
           pageWorkShopName = workList[0].workshopName;
         }
         res.pageWorkShopName = pageWorkShopName;
         res.pageDeptName = pageDeptName;
         var processingObjects = res.processingObject;
         if (processingObjects != null && processingObjects != '') {
           processingObjects = processingObjects.split(',');
         } else {
           processingObjects = [];
         }
         res.limit = processingObjects.length;
         res.processingObjects = processingObjects;
         var reviewerList = res.reviewerList;
         res.reviewerList = _.filter(reviewerList, function (rr) {
           return rr.approverId != 0;
         })
         res.reviewerUserId = _.map(reviewerList, "approverId");

         var qcReviewerList = res.qcReviewerList;
         var qcReviewerIds = '';
         if (!SysUtil.isEmpty(qcReviewerList)) {
           qcReviewerIds = [];
           for (var i = 0; i < qcReviewerList.length; i++) {
             var qcReviewer = qcReviewerList[i];
             var qcApproverId = qcReviewer.qcApproverId;
             if (qcApproverId === 0) {
               continue;
             }
             qcReviewerIds.push(qcApproverId);
           }
           if (qcReviewerIds.length === 0) {
             qcReviewerIds = '';
           }
         }

         res.qcReviewerIds = qcReviewerIds;
         self.updateProcessDetail = res;
       }).catch(function (error) {
         console.log(error);
       });
     },
     updateProcess: function () {
       var self = this;
       var processDetail = self.updateProcessDetail;
       processDetail = self.checkParam(processDetail, 'update');
       if (!processDetail) {
         return false;
       }
       ajaxRequest("procedure/updateProcessByCode", processDetail).then(function (res) {
         self.$message({
           showClose: true,
           message: res,
           type: 'success'
         });
         self.resetProcessList(false);
       }).catch(function (error) {
         console.log(error);
       });
     },
     addProcess: function () {
       var self = this;
       var processDetail = self.addProcessDetail;
       processDetail = self.checkParam(processDetail, 'add');
       if (!processDetail) {
         return false;
       }
       processDetail.reportMode = processDetail.reportModeRecord
       ajaxRequest("procedure/insertProcess", processDetail).then(function (res) {
         self.$message({
           showClose: true,
           message: res,
           type: 'success'
         });
         self.addDialog = false;
         self.resetProcessList(false);
       }).catch(function (error) {
         console.log(error);
       });
     },
     importProcess: function () {
       var self = this;
       if (self.adminProcessIdList.length <= 0) {
         self.$message({
           message: '请选择要导入的工序',
           type: 'warning'
         });
         return false;
       }
       ajaxRequest("basicData/importprocess", {
         'processStr': _.join(self.adminProcessIdList, ',')
       }).then(function (res) {
         self.$message({
           showClose: true,
           message: res,
           type: 'success'
         });
         self.importDialog = false;
         self.resetProcessList(false);
       }).catch(function (error) {
         console.log(error);
       });
     },
     checkParam: function (processDetail, type) {
       processDetail = _.cloneDeep(processDetail);
       var self = this;
       if ((processDetail.processName == '交接入库' || processDetail.processName == '交接出库')) {
         processDetail.processingObject = '所有';
       } else {
         //  limit
         var processingObjects = processDetail.processingObjects;
         if (processingObjects.length === 0) {
           self.$message({
             message: '请选择加工对象',
             type: 'warning'
           });
           return false;
         }
         if (type === 'add') {
           if (processingObjects.length > 1) {
             self.$message({
               message: '加工对象只能选择一个',
               type: 'warning'
             });
             return false;
           }
         } else {
           var limit = processDetail.limit;
           if (limit === 0) {
             limit = 1;
           }
           if (processingObjects.length > limit) {
             self.$message({
               message: '加工对象只能选择' + limit + '个',
               type: 'warning'
             });
             return false;
           }
         }
         processDetail.processingObject = _.join(processingObjects, ",");
       }
       if (processDetail.productionLine == '') {
         self.$message({
           message: '请选择部门',
           type: 'warning'
         });
         return false;
       }
       if (processDetail.workshopName == '') {
         self.$message({
           message: '请选择车间',
           type: 'warning'
         });
         return false;
       }
       if (processDetail.processName == '') {
         self.$message({
           message: '请输入工序名称',
           type: 'warning'
         });
         return false;
       }
       if (processDetail.status == '') {
         self.$message({
           message: '请选择状态',
           type: 'warning'
         });
         return false;
       }
       // if (processDetail.processingObject == "") {
       //   self.$message({
       //     message: '请选择加工对象',
       //     type: 'warning'
       //   });
       //   return false;
       // }
       if (processDetail.workingProcedureDuration < 0) {
         self.$message({
           message: '请输入大于等于0的工序作业时间',
           type: 'warning'
         });
         return false;
       }
       if (processDetail.handoverTime < 0) {
         self.$message({
           message: '请输入大于等于0的工序交接时间',
           type: 'warning'
         });
         return false;
       }
       if (processDetail.sort < 0) {
         self.$message({
           message: '请输入大于等于0的顺序',
           type: 'warning'
         });
         return false;
       }
       if (processDetail.unitPrice < 0) {
         self.$message({
           message: '请输入大于等于0的单价',
           type: 'warning'
         });
         return false;
       }
       if (processDetail.isNeedReviewed == 1) {
         var reviewerList = _.map(processDetail.reviewerList, function (o) {
           return {
             userId: o.approverId,
             userName: o.approverName
           };
         });
         processDetail.reviewers = JSON.stringify(reviewerList);

         var qcReviewerIds = processDetail.qcReviewerIds;
         var qcReviewers = [];
         var personData = this.personData;
         for (var i = 0; i < personData.length; i++) {
           var person = personData[i];
           var id = person.key;
           if (qcReviewerIds.indexOf(id) !== -1) {
             qcReviewers.push({
               userId: id,
               userName: person.label
             });
           }
         }
         processDetail.qcReviewers = JSON.stringify(qcReviewers);
       } else {
         processDetail.reviewers = null;
         processDetail.qcReviewers = null;
       }

       delete processDetail.qcReviewerIds;

       return processDetail;
     },
     qcFormSetting: function (type, processObj) {
       var selectProcess;
       if (type === 'update') {
         var obj = this.updateProcessDetail;
         selectProcess = {
           id: obj.id,
           processName: obj.processName,
           deptName: obj.pageDeptName,
           workshopName: obj.pageWorkShopName
         };
       } else {
         selectProcess = {
           id: Number(processObj.id),
           processName: processObj.processName,
           deptName: processObj.productionLine,
           workshopName: processObj.workshopName
         };
       }
       this.selectProcess = selectProcess;
       this.openSetting = true;
     },
     exportExcel: function () {
       var basePath = SysUtil.getBasePath();
       var path = `${basePath}/basicData/exportpRrocessDetail`;
       window.open(path);
     }
   },
   components: {
     QcFormSetting: QcFormSetting
   }
 });
